<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="履约流水号" prop="no" >
        <el-input v-model="formData.no" placeholder="请输入履约流水号"  disabled/>
      </el-form-item>
      <el-form-item label="发货日期" prop="shipDate" >
        <el-date-picker
          v-model="formData.shipDate"
          type="date"
          value-format="x"
          placeholder="选择发货日期"
          disabled
        />
      </el-form-item>
      <el-form-item label="合作机构" prop="memberOrganizeName" >
        <el-input v-model="formData.memberOrganizeName" placeholder="请选择合作机构" disabled/>
      </el-form-item>
      <!-- <el-form-item label="商品原价（总），单位：分" prop="totalPrice">
        <el-input v-model="formData.totalPrice" placeholder="请输入商品原价（总），单位：分" />
      </el-form-item> -->
      <el-form-item label="支付金额" prop="payPriceDecimal">
        <el-input v-model="formData.payPriceDecimal" placeholder="请输入应付支付金额" disabled />
      </el-form-item>
      <!-- <el-form-item label="履约单状态" prop="status">
        <el-select v-model="formData.status" placeholder="请选择履约单状态">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item> -->
      <el-form-item label="商品" prop="spuName">
        <el-input v-model="formData.spuName" placeholder="请输入商品" disabled />
      </el-form-item>
      <!-- <el-form-item label="sku_id" prop="skuId">
        <el-input v-model="formData.skuId" placeholder="请输入sku_id" />
      </el-form-item> -->
      <el-form-item label="数量" prop="count">
        <el-input v-model="formData.count" placeholder="请输入数量" disabled />
      </el-form-item>
      <el-form-item label="实出的数量" prop="actualCount">
        <el-input v-model="formData.actualCount" placeholder="请输入实出的数量" />
      </el-form-item>
      <el-form-item label="实出的价格" prop="actualPriceDecimal">
        <el-input v-model="formData.actualPriceDecimal" placeholder="请输入实出的价格" disabled />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { dateFormatter2 } from '@/utils/formatTime'
import { OrderApi, OrderVO } from '@/api/fulfill/order'

/** 履约单 表单 */
defineOptions({ name: 'OrderForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  no: undefined,
  shipDate: undefined,
  memberOrganizeId: undefined,
  memberOrganizeName: undefined,
  // totalPrice: undefined,
  payPrice: undefined,
  payPriceDecimal: undefined,
  status: undefined,
  spuId: undefined,
  spuName: undefined,
  // skuId: undefined,
  count: undefined,
  actualCount: undefined,
  actualPrice: undefined
})
const formRules = reactive({
  no: [{ required: true, message: '履约流水号不能为空', trigger: 'blur' }],
  shipDate: [{ required: true, message: '发货日期', trigger: 'blur' }],
  memberOrganizeName: [{ required: true, message: '合作机构不能为空', trigger: 'change' }],
  // totalPrice: [{ required: true, message: '商品原价（总），单位：分不能为空', trigger: 'blur' }],
  payPriceDecimal: [{ required: true, message: '支付金额', trigger: 'blur' }],
  status: [{ required: true, message: '履约单状态不能为空', trigger: 'change' }],
  spuName: [{ required: true, message: '商品不能为空', trigger: 'blur' }],
  // skuId: [{ required: true, message: 'sku_id不能为空', trigger: 'blur' }],
  count: [{ required: true, message: '数量不能为空', trigger: 'blur' }],
  actualCount: [{ required: true, message: '实出的数量不允许为空', trigger: 'blur' }],
  actualPrice: [{ required: true, message: '实出价格', trigger: 'blur' }]

})
const formRef = ref() // 表单 Ref

// /** 子表的表单 */
// const subTabsName = ref('orderItem')
// const orderItemFormRef = ref()

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await OrderApi.getOrder(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()

  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as OrderApi.OrderVO
    // // 拼接子表的数据
    // data.orderItems = orderItemFormRef.value.getData()
    if (formType.value === 'create') {
      await OrderApi.createOrder(data)
      message.success(t('common.createSuccess'))
    } else {
      await OrderApi.updateOrder(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    no: undefined,
    shipDate: undefined,
    memberOrganizeId: undefined,
    // totalPrice: undefined,
    payPrice: undefined,
    payPriceDecimal: undefined,
    status: undefined,
    spuId: undefined,
    spuName: undefined,
    memberOrganizeName: undefined,
    // skuId: undefined,
    count: undefined,
    actualCount: undefined,
    actualPrice: undefined
  }
  formRef.value?.resetFields()
}
</script>